package com.grad.webstudy.controller;

import com.grad.webstudy.model.es.ESCourse;
import com.grad.webstudy.model.es.ESLog;
import com.grad.webstudy.service.ElasticSearchService;
import com.grad.webstudy.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Author: zjf
 * @Date: 2020/4/2 8:43.
 */

@RestController
@RequestMapping("/es")
public class ElasticSearchController {


    @Autowired
    private ElasticSearchService elasticSearchService;


    @Autowired
    private RedisService redisService;

    @GetMapping("/search/queryCourseByKeyWord")
    public Page<ESCourse> queryCourseByKeyWord(@RequestParam(required = true) String keyword,
                                               @RequestParam(defaultValue = "1") Integer currPage){

        redisService.addHotWord(keyword);
        return elasticSearchService.queryByKeyWord(keyword, currPage);
    }


    @GetMapping("/queryLog")
    public AggregatedPage<ESLog> queryLog(@RequestParam(defaultValue = "1") Integer currPage,
                                          @RequestParam(defaultValue = "10") Integer pageSize){
        return elasticSearchService.queryAllLog(currPage, pageSize);
    }
}
